//
//  ZanAnimation.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/8/3.
//  Copyright © 2020 lt. All rights reserved.
//

import UIKit

class ZanAnimation: NSObject {
    
    static let size: CGFloat = 70.0
    static let angleArr: [CGFloat] = [CGFloat.pi / 4.0, -CGFloat.pi / 4.0, 0.0]
    
    static func showAnimation(point: CGPoint, baseView: UIView) {
        
        let imgV = UIImageView.init(frame: CGRect.init(x: point.x - ZanAnimation.size / 2.0, y: point.y - ZanAnimation.size / 2.0, width: size, height: size))
        imgV.image = UIImage(named: "icon_like_btn_S")
        imgV.contentMode = .scaleAspectFill
        baseView.addSubview(imgV)
        
        // 偏移角度
        let num = Int(arc4random_uniform(3))
        imgV.transform = CGAffineTransform.init(rotationAngle: ZanAnimation.angleArr[num])
        // 放大动画
        let animation = CAKeyframeAnimation(keyPath: "transform.scale")
        animation.duration = 0.15;
        animation.calculationMode = CAAnimationCalculationMode.cubic;
        animation.values = [1.3, 0.8, 1.0]
        imgV.layer.add(animation, forKey: "transform.scale")
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            
            UIView.animate(withDuration: 0.5, animations: {
                
                imgV.alpha = 0.0
                var newFrame = imgV.frame
                newFrame.origin.x -= 5.0
                newFrame.origin.y -= 45.0
                newFrame.size.height += 10.0
                newFrame.size.width += 10.0
                imgV.frame = newFrame
                
            }, completion: { (isOK) in
                
                imgV.removeFromSuperview()
            })
        }
    }
}
